package sein.android.hopenote;

import java.io.ObjectOutputStream.PutField;
import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TableLayout;
import android.widget.TableRow;

public class Note_Input extends Activity implements OnClickListener{
	
	private LinearLayout buttonArea,lineArea;
	private final int BUTTON_ID = 0x8000;
	private	Cursor cur_group;
	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	private final int ArrayCnt = 3;
	private final int MaxButton = 9;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_input);
		//Log.i("test1", getParent().toString());
		//ButtonSet();
	}
	
	public void ButtonSet(){
		HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();
        
        Button userinput = (Button)findViewById(R.id.user_input);
        userinput.setOnClickListener(this);
        
        Button[] dynamicButton = new Button[MaxButton];
        
        cur_group = sdb.query(true, HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_GROUP}, null, null, null, null, null, null);
        
        cur_group.moveToFirst();
       
        TableLayout.LayoutParams linearParam;
        TableRow.LayoutParams buttonParam;
        
        //int buttonwidth = userinput.getWidth()/3;
        //Log.i("버트길이", Integer.toString( userinput.getWidth()));
        //Log.i("로우", Integer.toString(buttonwidth));
        linearParam = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT ,LayoutParams.FILL_PARENT);
        buttonParam = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT ,LayoutParams.WRAP_CONTENT);
        
        int i,k = 0;
        
        TableLayout tb = (TableLayout)findViewById(R.id.buttonTable);
        tb.removeAllViewsInLayout();
        
        for( i = 0; i < cur_group.getCount(); i++){
        	
	        	dynamicButton[i] = new Button(this);
	    		//dynamicButton[i].setId(BUTTON_ID+i);
	        	dynamicButton[i].setText(cur_group.getString(0));
	        	dynamicButton[i].setTag(cur_group.getString(0));
	        	
	        	dynamicButton[i].setOnClickListener(this);
	        	//Log.i("test",cur_group.getString(0));
	        	
	        	if ( (i % ArrayCnt == 0 && i!= 0))
	        	{	        		
	        		//k++;
	        		TableRow tr = new TableRow(this);
	        		tr.setLayoutParams(new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,android.widget.TableRow.LayoutParams.WRAP_CONTENT));
	        		//Log.i("로우", Integer.toString(i));
	        		//dynamicLine[k].setLayoutParams(linearParam);
	        		tr.addView(dynamicButton[i-3],buttonParam);
	        		tr.addView(dynamicButton[i-2],buttonParam);
	        		tr.addView(dynamicButton[i-1],buttonParam);
	        		
	        		tb.addView(tr,linearParam);
	        		
	        	}

	        	cur_group.moveToNext();

        }
        
        if ( cur_group.getCount() > 0 ){
	        TableRow tr = new TableRow(this);
	        tr.setLayoutParams(new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,android.widget.TableRow.LayoutParams.WRAP_CONTENT));
			
	        //Log.i("카운트 i", Integer.toString(i));
			//dynamicLine[k].setLayoutParams(linearParam);
	        if ( i % ArrayCnt == 0){
	        	tr.addView(dynamicButton[i-3],buttonParam);
	    		tr.addView(dynamicButton[i-2],buttonParam);
	    		tr.addView(dynamicButton[i-1],buttonParam);		
	        }
	        else if ( i % ArrayCnt == 2){
	    		tr.addView(dynamicButton[i-2],buttonParam);
	    		tr.addView(dynamicButton[i-1],buttonParam);		
	        } else  if ( i % ArrayCnt == 1){
	    		tr.addView(dynamicButton[i-1],buttonParam);		
	        }
	        	
			tb.addView(tr,linearParam);
        }
		cur_group.close();
		//Log.d("test", "여기");
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if(v.getTag().toString().equals("userinput"))
		{
			//사용자등록 페이지로 이동
			Intent intent = new Intent(Note_Input.this,User_Add.class);
			
			v = Note_Input_Tab.InputTabGroup.getLocalActivityManager().startActivity("User_Add", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			
			Note_Input_Tab.InputTabGroup.replaceView(v,"user_add");
		}
		else {
			// 노트 등록페이지로 이동 그룹명을 등록페이지로 전달
			Intent intent = new Intent(Note_Input.this,Note_Add.class);
			HopeNoteDb.CURRENT_GROUP = v.getTag().toString();
			//HopeNoteDb.set_NOTE_GROUP(v.getTag().toString());
			intent.putExtra(HopeNoteDb.EXTRA_NOTE_ADD_GROUP,v.getTag().toString());
			v = Note_Input_Tab.InputTabGroup.getLocalActivityManager().startActivity("Note_Add", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			
			Note_Input_Tab.InputTabGroup.replaceView(v,"note_add");
		}
		
	}
	
	/*
	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		ButtonSet();
	}
	
	
	@Override
	public void onContentChanged() {
		// TODO Auto-generated method stub
		super.onContentChanged();
		Log.i("group", "change");
		ButtonSet();
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Log.i("group", "back");
		ButtonSet();
	}
	*/
}
